package com.ruoyi.car.business.service.impl;

import com.ruoyi.car.business.domain.BusAppointment;
import com.ruoyi.car.business.domain.BusStatement;
import com.ruoyi.car.business.mapper.BusAppointmentMapper;
import com.ruoyi.car.business.service.IBusAppointmentService;
import com.ruoyi.car.business.service.IBusStatementService;
import com.ruoyi.common.core.text.Convert;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-26
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;
    @Autowired
    private IBusStatementService statementService;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment)
    {
        BusAppointment newObj = new BusAppointment();
        newObj.setCustomerName(busAppointment.getCustomerName());
        newObj.setCustomerPhone(busAppointment.getCustomerPhone());
        newObj.setAppointmentTime(busAppointment.getAppointmentTime());
        newObj.setLicensePlate(busAppointment.getLicensePlate());
        newObj.setCarSeries(busAppointment.getCarSeries());
        newObj.setServiceType(busAppointment.getServiceType());
        newObj.setInfo(busAppointment.getInfo());
        newObj.setCreateTime(new Date());
        return busAppointmentMapper.insertBusAppointment(newObj);
    }

    /**
     * 修改养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment)
    {
        BusAppointment oldObj = this.selectBusAppointmentById(busAppointment.getId());
        // 如果状态不是预约中,不能进行编辑操作
        if (!BusAppointment.STATUS_APPOINTMENT.equals(oldObj.getStatus())) {
            throw new BuilderException("只有状态为预约中的预约单才能进行编辑操作");
        }
        oldObj.setCustomerName(busAppointment.getCustomerName());
        oldObj.setCustomerPhone(busAppointment.getCustomerPhone());
        oldObj.setAppointmentTime(busAppointment.getAppointmentTime());
        oldObj.setLicensePlate(busAppointment.getLicensePlate());
        oldObj.setCarSeries(busAppointment.getCarSeries());
        oldObj.setServiceType(busAppointment.getServiceType());
        oldObj.setInfo(busAppointment.getInfo());
        return busAppointmentMapper.updateBusAppointment(oldObj);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(String ids)
    {
        return busAppointmentMapper.deleteBusAppointmentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    @Override
    public void cancel(Long id) {
        BusAppointment appointment = this.selectBusAppointmentById(id);
        if (BusAppointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            busAppointmentMapper.changStatus(id, BusAppointment.STATUS_CANCEL);
        } else {
            throw new BuilderException("非法操作");
        }
    }

    @Override
    public void arrival(Long id) {
        BusAppointment appointment = this.selectBusAppointmentById(id);
        if (BusAppointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            busAppointmentMapper.arrival(id, BusAppointment.STATUS_ARRIVAL, new Date());
        } else {
            throw new BuilderException("非法操作");
        }
    }

    @Override
    public void changeStatus(Long appointmentId, Integer status) {
        busAppointmentMapper.changStatus(appointmentId,status);
    }

    @Override
    @Transactional
    public BusStatement generateStatement(Long appointmentId) {
        BusAppointment appointment = this.selectBusAppointmentById(appointmentId);
        // 合理性校验
        if(BusAppointment.STATUS_ARRIVAL.equals(appointment.getStatus()) ||
                BusAppointment.STATUS_SETTLE.equals(appointment.getStatus()) ||
                BusAppointment.STATUS_PAID.equals(appointment.getStatus())){
            // 根据预约单id查询结算单对象
            BusStatement statement = statementService.getByAppointmentId(appointmentId);
            if(statement==null){
                // 从预约单中把信息保存到结算单
                statement = new BusStatement();
                statement.setCustomerName(appointment.getCustomerName());
                statement.setCustomerPhone(appointment.getCustomerPhone());
                statement.setServiceType(appointment.getServiceType());
                statement.setCarSeries(appointment.getCarSeries());
                statement.setAppointmentId(appointmentId);
                statement.setLicensePlate(appointment.getLicensePlate());
                statement.setInfo(appointment.getInfo());
                statement.setActualArrivalTime(appointment.getActualArrivalTime());
                statement.setCreateTime(new Date());
                statementService.saveForAppointment(statement);
                // 修改预约单状态(结算单生成)
                busAppointmentMapper.changStatus(appointmentId,BusAppointment.STATUS_SETTLE);
            }
            return statement;
        }else {
            throw new BuilderException("非法操作");
        }
    }
}
